package com.dev.base.expand.mvp;


import com.dev.base.able.BaseModel;
import com.dev.base.able.IBaseViewModel;
import com.dev.base.able.IModelLisenner;
import dev.base.ResourceTable;
import ohos.aafwk.ability.LifecycleObserver;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * MVP BasePresenter层基类
 *
 * @param <BM>
 */
public abstract class BasePresenter<BM extends BaseModel> {
    private Context mContext;
    private IModelLisenner iModelLisenner;
    private IBaseViewModel iBaseViewModel;
    private LifecycleObserver lifecycleObserver;
    private CommonDialog popupDialog;
    private static AnimatorProperty animatorProperty;

    public IBaseViewModel getiBasegetBasePresenter() {
        return iBaseViewModel;
    }

    public void setiBaseViewModel(IBaseViewModel iBaseViewModel) {
        this.iBaseViewModel = iBaseViewModel;
    }

    public void setiModelLisenner(IModelLisenner iModelLisenner) {
        this.iModelLisenner = iModelLisenner;
    }

    public void cleaner() {
        getBasePresenter().cleaner();
    }

    public BasePresenter(Context mContext) {

        this.mContext = mContext;
    }

    protected abstract BM getBasePresenter();

    public void showProgressDialog() {
        if (popupDialog == null) {
            Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_loading, null, true);

            Image image = (Image) component.findComponentById(ResourceTable.Id_img);
            image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            image.setBindStateChangedListener(new Component.BindStateChangedListener() {
                @Override
                public void onComponentBoundToWindow(Component component) {
                    animatorProperty = image.createAnimatorProperty();
                    animatorProperty.rotate(360).setDuration(1000).setLoopedCount(100).start();
                }

                @Override
                public void onComponentUnboundFromWindow(Component component) {

                }
            });
            popupDialog = new CommonDialog(mContext);
            popupDialog.setContentCustomComponent(component);
            popupDialog.setTransparent(true);
            popupDialog.setAlignment(LayoutAlignment.CENTER).setTransparent(true);
            popupDialog.setSize(400, 600);
        }
        popupDialog.show();
    }

    public void dismiss() {
        if (animatorProperty != null) {
            animatorProperty.stop();
            animatorProperty.reset();
        }
        if (popupDialog != null) {
            popupDialog.destroy();
        }
    }
}
